package com.vf.admin.client.controller;

import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.dao.AppMessageRecordDao;
import com.vf.admin.client.service.AppSetupService;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.miaodong.entity.AppMessageRecord;
import com.vf.admin.miaodong.entity.AppSetup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 系统公告  ---黑哲
 */
@Controller
@RequestMapping("/app/message")
public class AppMessageRecordController extends ClientBaseController {

    @Autowired
    AppMessageRecordDao appMessageRecordDao;
    @Autowired
    AppSetupService appSetupService;
    @Autowired
    RedisUtil redisUtil;
    /**
     * 获取首页系统公告
     *
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Result list() {
        List<AppMessageRecord> list = appMessageRecordDao.list();
        Map<String,Object> mm = new HashMap<>(16);
        mm.put("list",list);
        return sucessJSON(mm);
    }


    /**
     * 获取公告
     * 1 = 进入直播间公告
     * @return
     */
    @RequestMapping("/announcement")
    @ResponseBody
    public Result announcement(@RequestParam(name = "type",defaultValue = "1") Integer type){
        String key = "";
        if(type.equals(1)){
            key = RedisKeys.TEXT + "announcement";
        }else if(type.equals(2)) {
            key = RedisKeys.TEXT + "invitationrulesL";
        }else{
            return errorJSON("类型错误");
        }
        Object context = redisUtil.get(key);
        if(Objects.isNull(context)) {
            AppSetup appSetup = appSetupService.get();
            if (type.equals(1)) {
                context = appSetup.getHuodongguize();
            }
            if(type.equals(2)){
                context = appSetup.getInvitationrulesL();
            }
            redisUtil.set(key, context,24*60*60);
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("liveAnnouncement", context);
        return sucessJSON(hashMap,"成功");
    }

}
